package minov.parser.utils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

import minov.parser.ArgumentObject;

/**
 * Map, that collects arguments under they keys
 * It was needed to :
 * - get argument by it's key
 * - find key by argument
 * - return all argument in order, in what they was registered
 */
public class ArgumentMapper {

   public ArgumentMapper() {

   }

   /**
    * For finding argument by key
    */
   private HashMap<String, ArgumentObject> mapOfArguments =
         new HashMap<String, ArgumentObject>();

   /**
    * For finding key of argument
    */
   private HashMap<ArgumentObject, String> mapOfKeys =
         new HashMap<ArgumentObject, String>();

   /**
    * For retrieving all argument in order, in what they was registered
    */
   private Queue<ArgumentObject> registrationOrder =
         new LinkedList<ArgumentObject>();

   /**
    * Add new argument and key
    * 
    * @param key
    * @param argument
    */
   public void put(String key, ArgumentObject argument) {

      if (this.containsKey(key)) {
         return;
      }
      mapOfArguments.put(key, argument);

      if (mapOfKeys.containsKey(argument) == false) {
         mapOfKeys.put(argument, key);
         registrationOrder.add(argument);
      }

   }

   /**
    * @param key
    *           of argument
    * @return true, if key was found
    */
   public boolean containsKey(String key) {
      return mapOfArguments.containsKey(key);
   }

   /**
    * @param key
    *           of argument
    * @return argument stored under key
    */
   public ArgumentObject get(String key) {
      return mapOfArguments.get(key);
   }

   /**
    * @param argument
    * @return true, if argument is stored
    */
   public boolean containsValue(ArgumentObject argument) {
      return mapOfKeys.containsKey(argument);
   }

   /**
    * @param argument
    * @return key of argument
    */
   public String getKey(ArgumentObject argument) {
      return mapOfKeys.get(argument);
   }

   /**
    * @return all registered arguments in the same order, in which they were
    *         registered
    */
   public ArgumentObject[] getAllArguments() {
      return registrationOrder.toArray(new ArgumentObject[0]);
   }

}
